package com.rykj.qxj.server.services.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rykj.qxj.server.common.constants.ApiStatusEnum;
import com.rykj.qxj.server.common.constants.ApiVerifyStatusEnum;
import com.rykj.qxj.server.common.constants.ChangeTypeEnum;
import com.rykj.qxj.server.common.constants.DeleteStatusEnum;
import com.rykj.qxj.server.dao.ApiManageMapper;
import com.rykj.qxj.server.dao.ApiVersionManageMapper;
import com.rykj.qxj.server.model.entity.ApiManage;
import com.rykj.qxj.server.model.entity.ApiVersionManage;
import com.rykj.qxj.server.services.IApiVersionManageService;
import com.techindata.cacheredis.CacheCom;
import com.techindata.cacheredis.CacheExsite;

@Service
public class ApiVersionManageServiceImpl implements IApiVersionManageService {
	
	@Autowired
	private ApiVersionManageMapper apiVersionManageMapper;
	
	@Autowired
	private ApiManageMapper apiManageMapper;
	
	public PageInfo selectPageBySearchParams(Integer pageNum, Integer pageSize, String changeType,
			String apiVerifyStatus,Integer apiId) {
		    PageHelper.startPage(pageNum, pageSize);
	        Map<String,Object> paraMap = new HashMap<>();
	        paraMap.put("changeType",changeType);
	        paraMap.put("apiVerifyStatus",apiVerifyStatus);
	        paraMap.put("apiId", apiId);
	        List<ApiManage> apiManageList = apiVersionManageMapper.selectListBySearchParams(paraMap);
	        PageInfo pageInfo=new PageInfo(apiManageList);
	        return  pageInfo;
	}

	public ApiVersionManage selectByPrimaryKey(Integer id) {
		return apiVersionManageMapper.selectByPrimaryKey(id);
	}
	public List<ApiVersionManage> selectApiVersionManageListByApiId(Integer apiId) {
		return apiVersionManageMapper.selectApiVersionManageListByApiId(apiId);
	}
	
	public void verifyIngApiManage(Integer apiVersionId, String verifyStatus, String verifyBy, String verifyDesc) throws Exception {
		Map<String,Object> prarmMap=new HashMap<String,Object>();
		prarmMap.put("apiVersionId", apiVersionId);
		prarmMap.put("verifyStatus", verifyStatus);
		prarmMap.put("verifyBy", verifyBy);
		prarmMap.put("verifyDesc", verifyDesc);
		prarmMap.put("verifyTime", new Date());
		if(verifyStatus.equals(ApiVerifyStatusEnum.VERIFY_PASS.getStatus())) {
			//审核通过以后需要把同一个接口别的版本修改成禁用状态
			ApiVersionManage apiVersionManage = apiVersionManageMapper.selectByPrimaryKey(apiVersionId);
			if(apiVersionManage!=null) {
				 ApiManage apiMana = apiManageMapper.selectApiManageByPrimaryKey(apiVersionManage.getApiId());
				 String cacheKey=(apiMana.getRequestMethod().toLowerCase()+apiMana.getUrl()).replace("/", ":");
					if(CacheExsite.isExsite(cacheKey)) {
						//缓存中存在，当禁用接口的时候需要删除key
						CacheCom.del(cacheKey);
					}
				if(apiVersionManage.getChangeType().equals(ChangeTypeEnum.DELETE.getStatus())) {
					List<ApiVersionManage> aVersionManageList=apiVersionManageMapper.selectApiVersionManageListByApiId(apiVersionManage.getApiId());
					if(aVersionManageList!=null ||aVersionManageList.size()==1) {
						ApiManage apiManage = apiManageMapper.selectApiManageByPrimaryKey(apiVersionManage.getApiId());
						apiManage.setApiStatus(ApiStatusEnum.DISABLE.getStatus());
						apiManage.setDeleteStatus(DeleteStatusEnum.DELETE.getStatus());
						apiManageMapper.updateByPrimaryKeySelective(apiManage);
						
					}
					apiVersionManage.setApiVerifyStatus(verifyStatus);
					apiVersionManage.setVerifyBy(verifyBy);
					apiVersionManage.setVerifyDesc(verifyDesc);
					apiVersionManage.setVerifyTime(new Date());
					apiVersionManage.setApiVersionStatus(ApiStatusEnum.DISABLE.getStatus());
					apiVersionManage.setDeleteStatus(DeleteStatusEnum.DELETE.getStatus());
					apiVersionManageMapper.updateByPrimaryKeySelective(apiVersionManage);
				}
				List<ApiVersionManage> apiVersionManageList = apiVersionManageMapper.selectApiVersionManageListByApiId(apiVersionManage.getApiId());
				if(apiVersionManageList!=null ||apiVersionManageList.size()>0) {
					for (ApiVersionManage apiVersionManageTemp : apiVersionManageList) {
						apiVersionManageTemp.setApiVersionStatus(ApiStatusEnum.DISABLE.getStatus());
						apiVersionManageMapper.updateByPrimaryKeySelective(apiVersionManageTemp);
						
					}
				}
			}
			prarmMap.put("apiVersionStatus", ApiStatusEnum.ENABLE.getStatus());
			apiVersionManageMapper.verifyIngApiManage(prarmMap);
		}else {
			apiVersionManageMapper.verifyIngApiManage(prarmMap);
		}
	}
	
	public void deleteApiVersionManage(Integer apiVersionId) {
		ApiVersionManage apiVersionManage = apiVersionManageMapper.selectByPrimaryKey(apiVersionId);
		if(null!=apiVersionManage) {
			apiVersionManage.setDeleteStatus(DeleteStatusEnum.DELETE.getStatus());
			apiVersionManage.setApiVersionStatus(ApiStatusEnum.DISABLE.getStatus());
			apiVersionManage.setUpdateTime(new Date());
			apiVersionManage.setChangeType(ChangeTypeEnum.DELETE.getStatus());
			apiVersionManageMapper.updateByPrimaryKeySelective(apiVersionManage);
		}
	}

	public PageInfo selectApiVersionManagePageByApiId(Integer pageNum, Integer pageSize, Integer apiId) {
		PageHelper.startPage(pageNum, pageSize);
		List<ApiVersionManage> apiVersionManageList = apiVersionManageMapper.selectApiVersionManageListByApiId(apiId);
		PageInfo page=new PageInfo(apiVersionManageList);
		return page;
	}
	public void updateByPrimaryKeySelective(ApiVersionManage apiVersionManage) {
		 apiVersionManageMapper.updateByPrimaryKeySelective(apiVersionManage);
		
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void enableOrDisableOperation(Integer apiVersionId, String status) throws Exception {
		ApiVersionManage apiVersionManage = apiVersionManageMapper.selectByPrimaryKey(apiVersionId);
		ApiManage apiManage = apiManageMapper.selectApiManageByPrimaryKey(apiVersionManage.getApiId());
		 String cacheKey=(apiManage.getRequestMethod().toLowerCase()+apiManage.getUrl()).replace("/", ":");
		if(CacheExsite.isExsite(cacheKey)) {
				//缓存中存在，当禁用接口的时候需要删除key
				CacheCom.del(cacheKey);
			}
		if(status.equals(ApiStatusEnum.ENABLE.getStatus())) {
			ApiVersionManage oldApiVersionManage = apiVersionManageMapper.selectApiVersionManageByApiIdAndStatus(apiVersionManage.getApiId());
			if(null!=oldApiVersionManage) {
				//如果是启用，需要查询当前版本是否有启用的，有启用的需要禁用之后再启用当前版本
				oldApiVersionManage.setApiVersionStatus(ApiStatusEnum.DISABLE.getStatus());
				apiVersionManageMapper.updateByPrimaryKeySelective(oldApiVersionManage);
				
			}
			apiVersionManage.setApiVersionStatus(status);
			apiVersionManageMapper.updateByPrimaryKeySelective(apiVersionManage);
		}else {
			apiVersionManage.setApiVersionStatus(status);
			apiVersionManageMapper.updateByPrimaryKeySelective(apiVersionManage);
		}
	}

}
